package com.tju.resource.tool.bcrypt;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * 明文加密
 *
 * @USER: CLS
 */
@Component
public class BCryptPasswordEncoderUtil extends BCryptPasswordEncoder {


    //随机生成密钥
    public byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
    //构建
    public SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, key);


    @Override
    public String encode(CharSequence rawPassword) {
        return super.encode(rawPassword);
    }

    /**
     * 密码对比
     *
     * @param rawPassword
     * @param encodedPassword
     * @return
     */
    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        return super.matches(rawPassword, encodedPassword);
    }

    /**
     * BCrypt加密
     *
     * @param msg
     * @return
     */
    public static String encrypt(String msg) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return passwordEncoder.encode(msg);
    }

    /**
     * AES加密
     *
     * @param rule
     * @return
     */
    public String aesEncryption(String rule) {
        //加密为16进制表示
        return aes.encryptHex(rule);
    }

    /**
     * AES解密
     *
     * @param ciphertext
     * @return
     */
    public String aesDecrypt(String ciphertext) {
        //加密为16进制表示
        return aes.decryptStr(ciphertext, CharsetUtil.CHARSET_UTF_8);
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
    }
}
